#include "UtilQtStyle.h"

#include <QStyleFactory>
void UtilQtStyle::setQtStyle(const QString &style)
{
    qout << QStyleFactory::keys();
    QApplication::setStyle(QStyleFactory::create(style));
}


void UtilQtStyle::setHighDpiScale()
{
    qputenv("QT_SCALE_FACTOR", "1.0");
#if (QT_VERSION >= QT_VERSION_CHECK(6,0,0))
    QApplication::setHighDpiScaleFactorRoundingPolicy(Qt::HighDpiScaleFactorRoundingPolicy::Unset);
#else
    QApplication::setAttribute(Qt::AA_UseHighDpiPixmaps);
    QApplication::setAttribute(Qt::AA_EnableHighDpiScaling);
#endif
}


/**
 * \brief UtilQtStyle::setSizePolicy 设置窗口大小策略
 * \param wdiget
 * \param hor：enum Policy {
                    Fixed = 0,
                    Minimum = GrowFlag,
                    Maximum = ShrinkFlag,
                    Preferred = GrowFlag | ShrinkFlag,
                    MinimumExpanding = GrowFlag | ExpandFlag,
                    Expanding = GrowFlag | ShrinkFlag | ExpandFlag,
                    Ignored = ShrinkFlag | GrowFlag | IgnoreFlag
               };
**/
void UtilQtStyle::setSizePolicy(QWidget *wdiget,QSizePolicy::Policy hor,QSizePolicy::Policy vert)
{
    QSizePolicy sizePolicy1=wdiget->sizePolicy();
    sizePolicy1.setHorizontalPolicy(hor);
    sizePolicy1.setVerticalPolicy(vert);
    wdiget->setSizePolicy(sizePolicy1);
}

QFrame *UtilQtStyle::addVLine(const QString& HexColor, int w)
{
    QFrame *line = new QFrame();
    line->setFixedWidth(w);
    line->setStyleSheet(QString("background-color:%1;").arg(HexColor));

    QSizePolicy sizePolicy2=line->sizePolicy();
    sizePolicy2.setHorizontalPolicy(QSizePolicy::Expanding);
    line->setSizePolicy(sizePolicy2);

    return line;
}

QFrame *UtilQtStyle::addHLine(const QString& HexColor,int h)
{
    QFrame *line = new QFrame();
    line->setFixedHeight(h);
    line->setStyleSheet(QString("background-color:%1;").arg(HexColor));

    QSizePolicy sizePolicy2=line->sizePolicy();
    sizePolicy2.setHorizontalPolicy(QSizePolicy::Expanding);
    line->setSizePolicy(sizePolicy2);

    return line;
}

///
/// \brief Functions::setStyle   可设置整个程序的样式表也可设置单个窗口的样式表
/// \param style                样式表文件路径
/// \param parent               指定要设置样式表的窗口，默认参数为：nullptr
///
void UtilQtStyle::setStyleSheet(const QString &style,QWidget *parent)
{

    QFile qssFile(style);
    qssFile.open(QFile::ReadOnly);
    if(qssFile.isOpen())
    {
        QString qss = QString(qssFile.readAll());
        if(parent == nullptr)
        {
            qApp->setStyleSheet(qss);
        }
        else
        {
            parent->setStyleSheet(qss);
        }
        qssFile.close();
        return;
    }
    qout << "qss样式表文件加载失败";
}
